% plot_memory_gantt_from_csv.m  
% Plot memory allocation Gantt chart from CSV data
function plot_memory_gantt_from_csv(csv_file, output_dir, case_name)
    % Read CSV data
    data = readtable(csv_file);
    
    % Extract unique cache types
    cache_types = unique(data.cache_type);
    
    for ct = cache_types'
        % Filter data for current cache type
        type_data = data(data.cache_type == ct, :);
        
        if isempty(type_data)
            continue;
        end
        
        % Create simple memory usage visualization
        fig = figure('Name', sprintf('Memory Gantt - Cache Type %d', ct), ...
                     'Position', [100, 100, 1200, 800]);
        
        % Group allocations by buffer_id for visualization
        buffer_ids = unique(type_data.buffer_id);
        colors = jet(length(buffer_ids));
        
        hold on;
        for i = 1:length(buffer_ids)
            buf_data = type_data(type_data.buffer_id == buffer_ids(i), :);
            alloc_data = buf_data(strcmp(buf_data.operation, 'alloc'), :);
            
            if ~isempty(alloc_data)
                for j = 1:height(alloc_data)
                    % Simple rectangle for each allocation
                    rect_x = [alloc_data.step(j)-0.4, alloc_data.step(j)+0.4, ...
                             alloc_data.step(j)+0.4, alloc_data.step(j)-0.4];
                    rect_y = [alloc_data.start_addr(j), alloc_data.start_addr(j), ...
                             alloc_data.start_addr(j)+alloc_data.size(j), ...
                             alloc_data.start_addr(j)+alloc_data.size(j)];
                    patch(rect_x, rect_y, colors(i,:), 'EdgeColor', 'k', 'LineWidth', 0.5);
                end
            end
        end
        hold off;
        
        title(sprintf('Memory Allocation Gantt Chart - Cache Type %d', ct));
        xlabel('Step');
        ylabel('Memory Address');
        grid on;
        
        % Save figure
        filename = sprintf('%s_memory_gantt_cache%d.png', case_name, ct);
        filepath = fullfile(output_dir, filename);
        saveas(fig, filepath);
        
        fprintf('  Memory Gantt chart saved: %s\n', filepath);
        close(fig);
    end
end



